package ru.spies.requests;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Vector;

import javax.servlet.http.HttpServletRequest;

import ru.spies.dbwork.DBWork;
import ru.spies.entities.Agent;
import ru.spies.entities.Agentsandtask;
import ru.spies.entities.Country;
import ru.spies.entities.Department;
import ru.spies.entities.Document;
import ru.spies.entities.Importance;
import ru.spies.entities.Task;
import ru.spies.entities.TaskType;
import ru.spies.help.MyLogger;
import ru.spies.profileClasses.UserInfo;

public class AddRequestWork extends AbstractRequestWork{
	
	private static AddRequestWork statThis=null;
	
	private static Country lastCountry=null; 
	private static Department lastDepartment=null; 
	
	private AddRequestWork() {
		
	}

	public static AddRequestWork getInstance(){
		if (statThis==null){
			statThis= new AddRequestWork();
		}	
		return statThis;
	}
	
	public Country addCountry(HttpServletRequest request){
		Country country = new Country();
		country.setName(getParamInUTF8(request.getParameter("countryName")));
		country.setDepartments(new Vector<Department>());
		country.setRelation(request.getParameter("relation"));
		country.setTasks(new Vector<Task>());
		return country;
	}

	public Country addDepartment(HttpServletRequest request){
		Department dep;
		lastCountry= new Country();
		lastCountry.setParamForFindQuery(request.getParameterValues("countryName")[0]);
		lastCountry=(Country) DBWork.getInstance().findObjectFromTable(lastCountry);
		if (lastCountry==null){
			MyLogger.getInstance().getLog().error(MyLogger.CAN_T_FIND_COUNTRY_LIKE_THIS);
			return null;
		}
			
		dep=new Department();
		dep.setName(getParamInUTF8(request.getParameter("depName")));
		dep.setDescription(changeIfNull(getParamInUTF8(request.getParameter("description"))));
		SimpleDateFormat sdf = new SimpleDateFormat("dd.MM.yyyy");
		java.util.Date now= new java.util.Date();
		try {
			dep.setCreationDate(sdf.parse(sdf.format(now)));
		} catch (ParseException e) {
			MyLogger.getInstance().getLog().error(MyLogger.EXCEPTION,e);
			return null;
		}
		dep.setCountry(lastCountry);
		if (changeIfNull(request.getParameter("idResident"))==null){
			dep.setResident(null);
		} else {
			Agent agent = new Agent ();
			agent.setParamForFindQuery(request.getParameterValues("idResident")[0]);
			agent = (Agent) DBWork.getInstance().findObjectFromTable(agent);
			dep.setResident(agent);
		}
		dep.setAgents(new Vector<Agent>());
		lastCountry.addDepartment(dep);
		return lastCountry;
	}
	
	public Country addAgent(HttpServletRequest request){
		Agent ag;
		lastDepartment=new Department();
		lastDepartment.setParamForFindQuery(request.getParameter("depName"));
		lastDepartment=(Department) DBWork.getInstance().findObjectFromTable(lastDepartment);
		if (lastDepartment==null){
			MyLogger.getInstance().getLog().error(MyLogger.CAN_T_FIND_DEPARTMENT_LIKE_THIS);
			return null;
		}
		ag=new Agent();
		ag.setName(getParamInUTF8(request.getParameter("agentName")));
		ag.setCountRecruits(0);
		ag.setStatus(false);
		ag.setDepartment(lastDepartment);
		ag.setAgentsandtasks(new Vector<Agentsandtask>());
		Agent recruiter = null;
		if (changeIfNull(request.getParameter("idRecruiter"))!=null){
			recruiter = new Agent ();
			recruiter.setParamForFindQuery(request.getParameterValues("idRecruiter")[0]);
			recruiter = (Agent) DBWork.getInstance().findObjectFromTable(recruiter);
		}
		ag.setRecruiter(recruiter);
		ag.setDocuments(new Vector<Document>());
		lastDepartment.addAgent(ag);
		return lastDepartment.getCountry();
	}
	
	
	
	public Task addTask(HttpServletRequest request){
		String countryName=changeIfNull(request.getParameter("countryName"));
		String taskName=changeIfNull(getParamInUTF8(request.getParameter("taskName")));
		if ((countryName==null)||(taskName==null)){
			return null;
		}
		Task task;
		lastCountry=new Country();	
		lastCountry.setParamForFindQuery(countryName);
		lastCountry=(Country) DBWork.getInstance().findObjectFromTable(lastCountry);
		if (lastCountry==null){
			MyLogger.getInstance().getLog().error(MyLogger.CAN_T_FIND_COUNTRY_LIKE_THIS);
			return null;
		}
		Importance importance = new Importance();
		importance.setParamForFindQuery(changeIfNull(request.getParameter("importance")));
		importance = (Importance) DBWork.getInstance().findObjectFromTable(importance);
		TaskType taskType = new TaskType();
		taskType.setParamForFindQuery(changeIfNull(request.getParameter("taskType")));
		taskType= (TaskType) DBWork.getInstance().findObjectFromTable(taskType);
		task=new Task();
		task.setName(taskName);
		task.setAgentsandtasks(new Vector<Agentsandtask>());
		task.setDocuments(new Vector<Document>());
		task.setImportance(importance);
		task.setStatus("a");
		task.setTaskType(taskType);
		importance.addTask(task);
		lastCountry.addTask(task);
		return task;
	}
	
	public Document addDocument(HttpServletRequest request){
		Document doc = new Document();
		String taskId=changeIfNull(request.getParameter("task"));
		if (taskId!=null){
			Task task = new Task();
			task.setParamForFindQuery(taskId);
			task = (Task) DBWork.getInstance().findObjectFromTable(task);
			doc.setTask(task);
			task.addDocument(doc);
		}
		Importance importance = new Importance();
		importance.setParamForFindQuery(request.getParameter("importance"));
		importance = (Importance) DBWork.getInstance().findObjectFromTable(importance);	
		doc.setImportance(importance);
		importance.addDocument(doc);	
		doc.setTextLink(getParamInUTF8(request.getParameter("textLink")));
		doc.setAgent(UserInfo.getInstance().getMe().getAgent());
		return doc;
	}
	
	public Importance addImportance(HttpServletRequest request){
		Importance importance = new Importance();
		importance.setDocuments(new Vector<Document>());
		importance.setTasks(new Vector<Task>());
		importance.setType(getParamInUTF8(request.getParameter("type")));
		return importance;
	}
	
	public TaskType addTaskType(HttpServletRequest request){
		TaskType taskType = new TaskType();
		taskType.setTasks(new Vector<Task>());	
		taskType.setTypeName(getParamInUTF8(request.getParameter("type")));
		return taskType;
	}

	public Agentsandtask addAgentAndTask(HttpServletRequest request){
		Task task = new Task();
		task.setParamForFindQuery(request.getParameter("task"));
		task = (Task) DBWork.getInstance().findObjectFromTable(task);
		Agent agent = new Agent();
		agent.setParamForFindQuery(request.getParameter("agent"));
		agent = (Agent) DBWork.getInstance().findObjectFromTable(agent);
		Agentsandtask agAndType = new Agentsandtask();
		agent.addAgentsandtask(agAndType);
		task.addAgentsandtask(agAndType);
		
		return agAndType;
	}
	
	public Country getLastCountry(){
		Country retVal=lastCountry;
		lastCountry=null;
		return retVal;
	}
	
	public Department getLastDepartment(){
		Department retVal=lastDepartment;
		lastDepartment=null;
		return retVal;
	}
		
}
